package com.kc.web.controller.business;

import com.kc.business.domain.BizBusinessGoods;
import com.kc.business.domain.BizScenicBusiness;
import com.kc.business.service.IBizBusinessGoodsService;
import com.kc.business.service.IBizScenicBusinessService;
import com.kc.common.annotation.Log;
import com.kc.common.annotation.RepeatSubmit;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.BusinessType;
import com.kc.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 景区商家商品管理
 */
@Slf4j
@RestController
@RequestMapping("/business/scenicBusinessGoods")
public class BizBusinessGoodsController extends BaseController {
    @Autowired
    private IBizBusinessGoodsService businessGoodsService;
    @Resource
    private IBizScenicBusinessService scenicBusinessService;
    /**
     * 商品列表
     * @param businessGoods
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:scenicBusinessGoods:list')")
    @GetMapping("/list")
    public TableDataInfo getList(BizBusinessGoods businessGoods){
        startPage();
        return getDataTable(businessGoodsService.getList(businessGoods));
    }
    /**
     * 商品新增
     * @param businessGoods
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:goods:add')")
    @Log(title = "景区商家商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody BizBusinessGoods businessGoods) {
        if(businessGoods.getGoodsIntro() == null){
            businessGoods.setGoodsIntro("");
        }
        BizScenicBusiness bizScenicBusiness = scenicBusinessService.getBusinessInfoById(businessGoods.getBusinessId());
        if(bizScenicBusiness.getMainTypeId() == 2){
            String[] lables = {businessGoods.getRoomSize()+"M",businessGoods.getBedType(),businessGoods.getWindowType(),businessGoods.getBreakfastNum()};
            businessGoods.setGoodsLables(lables);
        }
        businessGoods.setGoodsLable(StringUtils.join(businessGoods.getGoodsLables(), ","));
        return toAjax(businessGoodsService.add(businessGoods));
    }
    /**
     * 商品修改
     * @param businessGoods
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:goods:edit')")
    @Log(title = "景区商家商品管理", businessType = BusinessType.INSERT)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@Validated @RequestBody BizBusinessGoods businessGoods) {
        if(businessGoods.getGoodsIntro() == null){
            businessGoods.setGoodsIntro("");
        }
        BizScenicBusiness bizScenicBusiness = scenicBusinessService.getBusinessInfoById(businessGoods.getBusinessId());
        if(bizScenicBusiness.getMainTypeId() == 2){
            String[] lables = {businessGoods.getRoomSize()+"M",businessGoods.getBedType(),businessGoods.getWindowType(),businessGoods.getBreakfastNum()};
            businessGoods.setGoodsLables(lables);
        }
        businessGoods.setGoodsLable(StringUtils.join(businessGoods.getGoodsLables(), ","));
        return toAjax(businessGoodsService.edit(businessGoods));
    }
    /**
     * 商品删除
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:goods:remove')")
    @Log(title = "景区商家商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Integer id) {
        BizBusinessGoods bizBusinessGoods = new BizBusinessGoods();
        bizBusinessGoods.setId(id);
        bizBusinessGoods = businessGoodsService.getListInfo(bizBusinessGoods);
        bizBusinessGoods.setIsDeleted(1);
        return toAjax(businessGoodsService.remove(bizBusinessGoods));
    }

}
